package com.icim.service.impl;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.icim.common.HealthManager;
import com.icim.common.HealthPo;
import com.icim.service.IHealthCheckService;
import com.icim.tx.Amssend.AmssenderI;
import com.icim.util.HttpClientUtil;
import com.icim.util.MqOpe;

public class HealthCheckService implements IHealthCheckService {

	private Logger logger = Logger.getLogger(HealthCheckService.class);

	private List<HealthPo> healthPoList;

	@Autowired
	private ShowStatusService showStatusService;

	@Resource
	HttpClientUtil httpClientUtil;
	@Resource
	HealthManager healthCheckConfigure;

	@Override
	public void queueDepthCheck() {
		if (!"Y".equals(healthCheckConfigure.getHealthCheckFlg())) {
			logger.info("not check health and send alarm");
			return;
		}
		for (HealthPo healthPo : healthPoList) {
			String queueName = healthPo.getQueueName();
			int alarmLimitDepth = healthPo.getAlarmLimitDepth();
			int currentDepth = 0;
			try {
				currentDepth = showStatusService.getQueueDepth(queueName);
			} catch (IOException e) {
				e.printStackTrace();
				logger.error(e.toString());
			}
			String isErrorQueue = healthPo.getErrorQueue();
			String mailToAdds = healthPo.getMail_toAdds();
			if (currentDepth > alarmLimitDepth) {
				if ("Y".equals(isErrorQueue)) {
					sendHighLevelAlarm(queueName, currentDepth, mailToAdds);
				} else {
					sendLowLevelAlarm(queueName, currentDepth, mailToAdds);
				}
			}
		}

	}

	private void sendHighLevelAlarm(String queueName, int currentDepth,
			String mailToAdds) {
		StringBuffer buffer = new StringBuffer(
				"历史抄送系统存在ERROR QUEUE，请立刻处理!~~~ \n");
		buffer.append("Queue[").append(queueName).append("],当前深度")
				.append(currentDepth);
		String subject = "历史抄送系统存在ERROR QUEUE，请立刻处理!  [" + queueName + "]，深度: " + currentDepth ;
		sendToAMS(buffer, subject, mailToAdds);
	}

	private void sendLowLevelAlarm(String queueName, int currentDepth,
			String mailToAdds) {
		StringBuffer buffer = new StringBuffer(
				"历史抄送系统队列消息深度已经超过警戒线，请立刻处理!~~~ \n");
		buffer.append("Queue[").append(queueName).append("],当前深度")
				.append(currentDepth);
		String subject = "历史抄送系统队列消息深度已经超过警戒线，请立刻处理![" + queueName + "] ，深度: " + currentDepth ;
		sendToAMS(buffer, subject, mailToAdds);
	}

	private void sendToAMS(StringBuffer buffer, String subject,
			String mailToAdds) {
		AmssenderI inTrx = new AmssenderI();
		inTrx.setMail_subject(subject);
		inTrx.setMail_text(buffer.toString());
		inTrx.setTrx_id("AMSSENDER");
		inTrx.setType_id("I");
		inTrx.setAction_flg("M");
		inTrx.setMail_toAdds(mailToAdds);
		String outMesg = httpClientUtil.postHttp(MqOpe.obj2Str(inTrx));

		// AmssenderO outTrx = new AmssenderO();
		// outMesg =
		// outMesg.replace("<ServiceResponse><response>SUCCESS</response><exception/><return>",
		// "").replace("</return></ServiceResponse>", "");
		// outTrx = (AmssenderO) MqOpe.inStr2Obj(outMesg, outTrx);

		if (outMesg.contains("0000000")) {
			logger.info(outMesg);
		} else {
			logger.error(outMesg);
		}
	}

	public List<HealthPo> getHealthPoList() {
		return healthPoList;
	}

	public void setHealthPoList(List<HealthPo> healthPoList) {
		this.healthPoList = healthPoList;
	}

}
